package com.chenggong.window.activity

import android.content.Context
import android.os.Bundle
import android.text.ClipboardManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.core.view.isVisible
import com.chenggong.log.LogDebugUtil
import com.chenggong.log.ViewLog
import com.chenggong.window.R
import com.chenggong.window.activity.module.ModuleAnimationActivity
import com.chenggong.window.activity.module.ModuleCustomActivity
import com.chenggong.window.activity.module.ModuleFfmpegActivity
import com.chenggong.window.activity.module.ModuleRetrofitActivity
import com.chenggong.window.base.BaseActivity
import com.chenggong.window.databinding.ActivityMainBinding

class MainActivity: BaseActivity<ActivityMainBinding>() {

    private lateinit var adapter: ArrayAdapter<String>
    override val layoutId: Int
        get() = R.layout.activity_main

    override fun initView(savedInstanceState: Bundle?) {

        val data = arrayOf(
            getString(R.string.module_custom_tool),
            getString(R.string.module_log_tool),
            getString(R.string.module_animation_tool),
            getString(R.string.module_retrofit_tool),
            getString(R.string.module_view_log),
            getString(R.string.ffmpeg),
        )
        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
        val listView = findViewById<ListView>(R.id.list)
        listView.adapter = adapter
        listView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
            onItemClick(position)
        }
        mDataBinding.ivClose.setOnClickListener {
            mDataBinding.llInstructions.isVisible = false
        }
        mDataBinding.ivCopy.setOnClickListener {
            copyString(mDataBinding.tvContent.text.toString())
        }

        LogDebugUtil.init(true)
        LogDebugUtil.createViewLog(this)
    }

    private fun onItemClick(position: Int) {
        val value = adapter.getItem(position)
        when(value){
            getString(R.string.module_custom_tool) -> {
                startActivity(ModuleCustomActivity::class.java)
                LogDebugUtil.i("测试1", getString(R.string.module_custom_tool))
            }
            getString(R.string.module_log_tool) -> {
                mDataBinding.llInstructions.isVisible = true
                mDataBinding.tvContent.text = LogDebugUtil.instructions()
                LogDebugUtil.i("测试2", getString(R.string.module_log_tool))
            }
            getString(R.string.module_animation_tool) -> {
                startActivity(ModuleAnimationActivity::class.java)
                LogDebugUtil.e("测试3", getString(R.string.module_animation_tool))
            }
            getString(R.string.module_retrofit_tool) -> {
                startActivity(ModuleRetrofitActivity::class.java)
                LogDebugUtil.v("测试3", getString(R.string.module_retrofit_tool) )
            }
            getString(R.string.module_view_log) -> {
                LogDebugUtil.v("测试4", getString(R.string.module_view_log) )
                LogDebugUtil.showViewLog()
            }
            getString(R.string.ffmpeg) -> {
                startActivity(ModuleFfmpegActivity::class.java)
                LogDebugUtil.w("测试4", getString(R.string.ffmpeg) )
            }
        }
        LogDebugUtil.d(value)
    }

    override fun onBackPressed() {
        if(mDataBinding.llInstructions.isVisible){
            mDataBinding.llInstructions.isVisible = false
            return
        }
        super.onBackPressed()
    }

    private fun copyString(content: String) {
        val cm = this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        cm.text = content
        Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show()
    }
}